package ssm.dao.impl;

import ssm.dao.Search;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AbstractFactorySearch implements Search {
    /**
     * 抽象工厂模式
     *
     * @param conn
     * @param rs
     * @param rowsNumber
     * @throws SQLException
     */
    @Override
    public void check(Connection conn, ResultSet rs, int rowsNumber) throws SQLException {
        int count = 0;
        for (int i = 1; i <= rowsNumber; i++) {
            rs.absolute(i);
            String rel_type = rs.getString("RelationType");
            if (rel_type.equalsIgnoreCase("aggregation")) {

                String s1 = rs.getString("SourceClass");
                String d1 = rs.getString("DestinationClass");


                for (int j = 1; j <= rowsNumber; j++) {
                    rs.absolute(j);
                    String rel_type2 = rs.getString("RelationType");

                    if (rel_type2.equalsIgnoreCase("aggregation")) {

                        String s2 = rs.getString("SourceClass");
                        String d2 = rs.getString("DestinationClass");

                        if ((s1.equalsIgnoreCase(s2)) && !(d1.equalsIgnoreCase(d2))) {
/*                            if (s1.equalsIgnoreCase(s2)) {
                                String tempAbstractProductA = d1;
                                String tempAbstarctProductB = d2;
                            }*/
                            for (int k = 1; k <= rowsNumber; k++) {
                                rs.absolute(k);
                                String rel_type3 = rs.getString("relationtype");
                                if (rel_type3.equalsIgnoreCase("REALIZATION")) {
                                    String s3 = rs.getString("SourceClass");
                                    String d3 = rs.getString("DestinationClass");

                                    String tempConcreteFactory = s3;

                                    for (int l = 1; l <= rowsNumber; l++) {
                                        rs.absolute(l);
                                        String rel_type4 = rs.getString("relationtype");
                                        if (rel_type4.equalsIgnoreCase("REALIZATION")) {
                                            String s4 = rs.getString("SourceClass");
                                            String d4 = rs.getString("DestinationClass");
                                            if (d4.equalsIgnoreCase(d1) && !(d4.equalsIgnoreCase(d3))) {

                                                for (int m = 1; m <= rowsNumber; m++) {
                                                    rs.absolute(m);
                                                    String rel_type5 = rs.getString("relationtype");
                                                    if (rel_type5.equalsIgnoreCase("REALIZATION")) {
                                                        String s5 = rs.getString("SourceClass");
                                                        String d5 = rs.getString("DestinationClass");
                                                        if (d2.equalsIgnoreCase(d5) && !(d5.equalsIgnoreCase(d3))) {
                                                            String tempProdcutA = s4;
                                                            String tempProductB = s5;

                                                            for (int n = 1; n <= rowsNumber; n++) {
                                                                rs.absolute(n);
                                                                String rel_type6 = rs.getString("relationtype");
                                                                if (rel_type6.equalsIgnoreCase("ASSOCIATION")) {
                                                                    String s6 = rs.getString("SourceClass");
                                                                    String d6 = rs.getString("DestinationClass");
                                                                    if ((d6.equalsIgnoreCase(s4) || d6.equalsIgnoreCase(s5)) && s6.equalsIgnoreCase(s3)) {

                                                                        String sql2 = "INSERT INTO designs.abstractFactory_instances(AbstractProductA,AbstractProductB,AbstractFactory,ConcreteFactory,ProductA,ProductB) VALUES (?,?,?,?,?,?)";
                                                                        PreparedStatement stmt2 = conn.prepareStatement(sql2);

                                                                        stmt2.setString(1, d1);
                                                                        stmt2.setString(2, d2);
                                                                        stmt2.setString(3, d3);
                                                                        stmt2.setString(4, s3);
                                                                        stmt2.setString(5, s4);
                                                                        stmt2.setString(6, s5);

                                                                        int rows2 = stmt2.executeUpdate();
                                                                        count += rows2;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println("found " + count + " AbstractFactory");
    }
}
